package com.sc.nft.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.sc.nft.dao.*;
import com.sc.nft.dto.ActivityTaskPrizeDTO;
import com.sc.nft.dto.TaskPrizeDTO;
import com.sc.nft.dto.UserActivityTaskDTO;
import com.sc.nft.entity.*;
import com.sc.nft.enums.*;
import com.sc.nft.enums.task.ActivityTaskTypeEnum;
import com.sc.nft.enums.task.ParticipantsEnum;
import com.sc.nft.enums.task.TaskTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.ActivityTaskService;
import com.sc.nft.service.UserAssetsService;
import com.sc.nft.service.UserEquityPropsService;
import com.sc.nft.sup.ErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zxy
 * @since 2023-09-13
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ActivityTaskServiceImpl implements ActivityTaskService {
    private final ActivityTaskDao activityTaskDao;
    private final EquityPropsDao equityPropsDao;
    private final ActivityTaskPanelDao activityTaskPanelDao;
    private final ActivityTaskPlanDao activityTaskPlanDao;
    private final ActivityTaskPrizeDao activityTaskPrizeDao;
    private final DigitalCollectionDao digitalCollectionDao;
    private final UserCollectionDao userCollectionDao;
    private final FilterIncomeListDao filterIncomeListDao;
    private final UserAssetsService userAssetsService;
    private final UserEquityPropsService userEquityPropsService;
    private final Redisson redisson;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addTask(String name, Long taskPanelId, Long buyCollectionId, Integer buyCount, Long partakeCollectionId, ParticipantsEnum participants, Boolean collectionRange, Boolean mallRange, Integer taskPlan, Boolean repeat, List<ActivityTaskPrizeDTO> taskPrizeDTOS, Integer sort, BigDecimal spendPrice) {
        ActivityTaskPanel taskPanel = activityTaskPanelDao.getById(taskPanelId);
        Assert.notNull(taskPanel, () -> new GlobalRunTimeException("任务模板不存在"));
        Assert.isTrue(taskPanel.getStatus(), () -> new GlobalRunTimeException("任务模板已下架"));

        ActivityTask task = new ActivityTask();
        task.setTaskPanelId(taskPanelId);
        task.setTaskName(name);
        task.setType(taskPanel.getType());
        task.setStatus(true);
        task.setSort(sort);
        task.setRepeatFinish(repeat);
        task.setTaskPlan(taskPlan);
        if (task.getParticipants() == ParticipantsEnum.APPOINT && partakeCollectionId == null) {
            throw new GlobalRunTimeException("请选择参与商品");
        }
        task.setParticipants(participants);
        task.setPartakeCollectionId(partakeCollectionId);
        switch (task.getType()) {
            case BUY_APPOINT:
                DigitalCollection digitalCollection = digitalCollectionDao.getById(buyCollectionId);
                Assert.notNull(digitalCollection, () -> new GlobalRunTimeException(ErrorCode.COLLECTION_NOT_FOUND));
                Assert.isTrue(buyCount != null && buyCount > 0, () -> new GlobalRunTimeException("购买数量不正确"));
                task.setBuyCollectionId(buyCollectionId);
                task.setBuyCount(buyCount);
                break;
            case TOTAL_BUY:
                Assert.isTrue(buyCount != null && buyCount > 0, () -> new GlobalRunTimeException("购买数量不正确"));
                task.setBuyCount(buyCount);
                break;
            case TOTAL_SPEND:
                if (collectionRange == null && mallRange == null) {
                    throw new GlobalRunTimeException("请选择任务范围");
                }
                task.setSpendPrice(spendPrice);
                task.setCollectionRange(BooleanUtil.isTrue(collectionRange));
                task.setMallRange(BooleanUtil.isTrue(mallRange));
                break;
            default:
                throw new GlobalRunTimeException("任务类型不正确");
        }
        task.insert();
        insertTaskPrize(taskPrizeDTOS, task);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editTask(Long id, String name, Long taskPanelId, Long buyCollectionId, Integer buyCount, Long partakeCollectionId, ParticipantsEnum participants, Boolean collectionRange, Boolean mallRange, Integer taskPlan, Boolean repeat, List<ActivityTaskPrizeDTO> taskPrizeDTOS, Integer sort, BigDecimal spendPrice) {
        ActivityTask task = activityTaskDao.getById(id);
        Assert.notNull(task, () -> new GlobalRunTimeException(ErrorCode.TASK_NOT_FOUND));
        ActivityTaskPanel taskPanel = activityTaskPanelDao.getById(taskPanelId);
        Assert.notNull(taskPanel, () -> new GlobalRunTimeException("任务模板不存在"));
        Assert.isTrue(taskPanel.getStatus(), () -> new GlobalRunTimeException("任务模板已下架"));

        task.setTaskPanelId(taskPanelId);
        task.setTaskName(name);
        task.setType(taskPanel.getType());
        task.setStatus(true);
        task.setSort(sort);
        task.setRepeatFinish(repeat);
        task.setTaskPlan(taskPlan);

        if (task.getParticipants() == ParticipantsEnum.APPOINT && partakeCollectionId == null) {
            throw new GlobalRunTimeException("请选择参与商品");
        }
        task.setParticipants(participants);
        task.setPartakeCollectionId(partakeCollectionId);
        switch (task.getType()) {
            case BUY_APPOINT:
                DigitalCollection digitalCollection = digitalCollectionDao.getById(buyCollectionId);
                Assert.notNull(digitalCollection, () -> new GlobalRunTimeException(ErrorCode.COLLECTION_NOT_FOUND));
                Assert.isTrue(buyCount != null && buyCount > 0, () -> new GlobalRunTimeException("购买数量不正确"));
                task.setBuyCollectionId(buyCollectionId);
                task.setBuyCount(buyCount);
                break;
            case TOTAL_BUY:
                Assert.isTrue(buyCount != null && buyCount > 0, () -> new GlobalRunTimeException("购买数量不正确"));
                task.setBuyCount(buyCount);
                break;
            case TOTAL_SPEND:
                if (collectionRange == null && mallRange == null) {
                    throw new GlobalRunTimeException("请选择任务范围");
                }
                task.setSpendPrice(spendPrice);
                task.setCollectionRange(BooleanUtil.isTrue(collectionRange));
                task.setMallRange(BooleanUtil.isTrue(mallRange));
                break;
            default:
                throw new GlobalRunTimeException("任务类型不正确");
        }
        task.updateById();
        activityTaskPrizeDao.removeByTaskId(task.getId());
        insertTaskPrize(taskPrizeDTOS, task);
    }

    @Override
    public Page<ActivityTask> getPage(Page toMp) {
        return activityTaskDao.page(toMp);
    }

    @Override
    public ActivityTask getById(Long id) {
        return activityTaskDao.getById(id);
    }

    @Override
    public List<ActivityTask> list() {
        return activityTaskDao.list();
    }

    @Override
    public List<UserActivityTaskDTO> getActivityTask(UserInfo userInfo, Long activityId) {
        List<ActivityTask> taskList = activityTaskDao.getTaskByActivityId(activityId);
        List<UserActivityTaskDTO> userActivityTaskDTOS = Lists.newArrayList();
        for (ActivityTask task : taskList) {
            UserActivityTaskDTO userActivityTaskDTO = new UserActivityTaskDTO();
            userActivityTaskDTO.setTaskName(task.getTaskName());
            userActivityTaskDTO.setTaskType(task.getType());
            userActivityTaskDTO.setNeedCollection(task.getParticipants() == ParticipantsEnum.APPOINT);
            if (userActivityTaskDTO.getNeedCollection()) {
                DigitalCollection needCollection = digitalCollectionDao.getById(task.getPartakeCollectionId());
                userActivityTaskDTO.setCollectionName(needCollection.getFirstTitle());
                userActivityTaskDTO.setHoldCollection(userCollectionDao.getByUserIdAndCollectionIdCount(userInfo.getId(), task.getPartakeCollectionId()) > 0);

            }
            userActivityTaskDTO.setMonReset(task.getTaskPlan().intValue() == 1);
            if (userActivityTaskDTO.getMonReset()) {
                userActivityTaskDTO.setResetText("每周一重置");
            }
            if (!task.getRepeatFinish()) {
                userActivityTaskDTO.setFinishStatus(activityTaskPlanDao.getFinishCount(task.getId(), userInfo.getId()) > 0);
            } else {
                userActivityTaskDTO.setFinishStatus(false);
            }
            List<ActivityTaskPrizeDTO> taskPrizeDTOS = Lists.newArrayList();
            List<ActivityTaskPrize> prizeList = activityTaskPrizeDao.getByTaskId(task.getId());
            for (ActivityTaskPrize activityTaskPrize : prizeList) {
                ActivityTaskPrizeDTO prizeDTO = new ActivityTaskPrizeDTO();
                prizeDTO.setType(activityTaskPrize.getType());
                prizeDTO.setPropsId(activityTaskPrize.getEquityId());
                prizeDTO.setPropsName(activityTaskPrize.getEquityName());
                prizeDTO.setPrize(activityTaskPrize.getPrize());
                taskPrizeDTOS.add(prizeDTO);
            }
            ActivityTaskPlan goingTask = activityTaskPlanDao.getGoingTask(task.getId(), userInfo.getId());
            switch (task.getType()) {
                case BUY_APPOINT:
                    userActivityTaskDTO.setTaskCurrentPlan(ObjectUtil.isNull(goingTask) ? "0" : goingTask.getTaskCurrentPlan().stripTrailingZeros().toPlainString());
                    userActivityTaskDTO.setTaskFinishCondition(task.getBuyCount().toString());
                    break;
                case TOTAL_BUY:
                    userActivityTaskDTO.setTaskCurrentPlan(ObjectUtil.isNull(goingTask) ? "0" : goingTask.getTaskCurrentPlan().stripTrailingZeros().toPlainString());
                    userActivityTaskDTO.setTaskFinishCondition(task.getBuyCount().toString());
                    break;
                case TOTAL_SPEND:
                    userActivityTaskDTO.setTaskCurrentPlan(ObjectUtil.isNull(goingTask) ? "0" : goingTask.getTaskCurrentPlan().toPlainString());
                    userActivityTaskDTO.setTaskFinishCondition(task.getSpendPrice().toPlainString());
                    break;
            }
            userActivityTaskDTO.setTaskPrizeDTOS(taskPrizeDTOS);
            userActivityTaskDTOS.add(userActivityTaskDTO);
        }
        return userActivityTaskDTOS;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void finishTask(Long collectionId, Long userId, int count, BigDecimal price, ActivityTaskTypeEnum taskType, LinkTypeEnums linkType, Long linkId,Boolean isMallOrder) {
        if(filterIncomeListDao.getIsContainsUserId(userId)){
            return;
        }

        // 2024-02-20 新增任务锁,用锁key+userId+类型作为锁的key,拿来处理在前一个请求没有解决的时候,后一个请求又一次进入,导致了并发数据库多条数据的问题
        String key = StrUtil.format("{}{}_{}", RedisKeyEnum.FINISH_TASK_KEY_, userId, taskType);
        RLock lock = redisson.getLock(key);
        lock.lock();
        try {
            DateTime now = DateTime.now();

            List<ActivityTask> activityTasks = activityTaskDao.goingTask(taskType, collectionId);
            for (ActivityTask activityTask : activityTasks) {
                int lastCount = 0;
                BigDecimal lastPrice = BigDecimal.ZERO;
                if (!activityTask.getRepeatFinish() && activityTaskPlanDao.getFinishCount(activityTask.getId(), userId) > 0) {
                    continue;
                }
                if (activityTask.getType() == ActivityTaskTypeEnum.BUY_APPOINT && collectionId.longValue() != activityTask.getBuyCollectionId().longValue()) {
                    continue;
                }
                if (activityTask.getParticipants() == ParticipantsEnum.APPOINT && userCollectionDao.getByUserIdAndCollectionIdCount(userId, activityTask.getPartakeCollectionId()) == 0) {
                    continue;
                }

                if (activityTask.getType() == ActivityTaskTypeEnum.TOTAL_SPEND) {
                    if (isMallOrder) {
                        if (!activityTask.getMallRange()) {
                            continue;
                        }
                    } else {
                        if (!activityTask.getCollectionRange()) {
                            continue;
                        }
                    }
                }

                ActivityTaskPlan goingTaskPlan = activityTaskPlanDao.getGoingTask(activityTask.getId(), userId);
                if (ObjectUtil.isNull(goingTaskPlan)) {
                    goingTaskPlan = new ActivityTaskPlan();
                    goingTaskPlan.setUserId(userId);
                    goingTaskPlan.setTaskName(activityTask.getTaskName());
                    goingTaskPlan.setActivityTaskId(activityTask.getId());
                    goingTaskPlan.setTaskType(activityTask.getType());
                    goingTaskPlan.setTaskCurrentPlan(BigDecimal.ZERO);
                    switch (taskType) {
                        case BUY_APPOINT:
                            goingTaskPlan.setTaskFinishCondition(BigDecimal.valueOf(activityTask.getBuyCount()));
                            break;
                        case TOTAL_BUY:
                            goingTaskPlan.setTaskFinishCondition(BigDecimal.valueOf(activityTask.getBuyCount()));
                            break;
                        case TOTAL_SPEND:
                            goingTaskPlan.setTaskFinishCondition(activityTask.getSpendPrice());
                            break;
                    }
                    goingTaskPlan.setTaskDate(now);
                    if (activityTask.getTaskPlan().intValue() == 0) {
                        goingTaskPlan.setTaskResetTime(now.offsetNew(DateField.YEAR, 100));
                    } else {
                        goingTaskPlan.setTaskResetTime(DateUtil.beginOfWeek(now.offsetNew(DateField.DAY_OF_MONTH, 7)));
                    }
                    goingTaskPlan.setStatus(false);
                    goingTaskPlan.insert();
                }
                ActivityTaskPlanRecord activityTaskPlanRecord = new ActivityTaskPlanRecord();
                activityTaskPlanRecord.setActivityTaskId(activityTask.getId());
                activityTaskPlanRecord.setActivityTaskPlanId(goingTaskPlan.getId());
                activityTaskPlanRecord.setUserId(userId);
                activityTaskPlanRecord.setPlanTime(now);
                activityTaskPlanRecord.setLinkType(linkType);
                activityTaskPlanRecord.setLinkId(linkId);
                activityTaskPlanRecord.insert();
                switch (taskType) {

                    case BUY_APPOINT:
                        if (NumberUtil.isGreater(goingTaskPlan.getTaskCurrentPlan().add(BigDecimal.valueOf(count)), goingTaskPlan.getTaskFinishCondition())) {
                            lastCount = goingTaskPlan.getTaskCurrentPlan().add(BigDecimal.valueOf(count)).subtract(goingTaskPlan.getTaskFinishCondition()).intValue();
                        }
                        goingTaskPlan.setTaskCurrentPlan(NumberUtil.min(goingTaskPlan.getTaskCurrentPlan().add(BigDecimal.valueOf(count)), goingTaskPlan.getTaskFinishCondition()));
                        break;
                    case TOTAL_BUY:
                        if (NumberUtil.isGreater(goingTaskPlan.getTaskCurrentPlan().add(BigDecimal.valueOf(count)), goingTaskPlan.getTaskFinishCondition())) {
                            lastCount = goingTaskPlan.getTaskCurrentPlan().add(BigDecimal.valueOf(count)).subtract(goingTaskPlan.getTaskFinishCondition()).intValue();
                        }
                        goingTaskPlan.setTaskCurrentPlan(NumberUtil.min(goingTaskPlan.getTaskCurrentPlan().add(BigDecimal.valueOf(count)), goingTaskPlan.getTaskFinishCondition()));
                        break;
                    case TOTAL_SPEND:
                        if (NumberUtil.isGreater(goingTaskPlan.getTaskCurrentPlan().add(price), goingTaskPlan.getTaskFinishCondition())) {
                            lastPrice = goingTaskPlan.getTaskCurrentPlan().add(price).subtract(goingTaskPlan.getTaskFinishCondition());
                        }
                        goingTaskPlan.setTaskCurrentPlan(NumberUtil.min(goingTaskPlan.getTaskCurrentPlan().add(price), goingTaskPlan.getTaskFinishCondition()));
                        break;
                }
                if (NumberUtil.isGreaterOrEqual(goingTaskPlan.getTaskCurrentPlan(), goingTaskPlan.getTaskFinishCondition())) {
                    goingTaskPlan.setStatus(true);
                    goingTaskPlan.setTaskFinishTime(now);
                }
                goingTaskPlan.updateById();

                //发放奖励
                if (goingTaskPlan.getStatus()) {
                    sendPrize(activityTask, goingTaskPlan);
                }

                switch (taskType) {
                    case BUY_APPOINT:
                        if (lastCount > 0) {
                            finishTaskSub(activityTask, collectionId, userId, lastCount, lastPrice, taskType, linkType, linkId);
                        }
                        break;
                    case TOTAL_BUY:
                        if (lastCount > 0) {
                            finishTaskSub(activityTask, collectionId, userId, lastCount, lastPrice, taskType, linkType, linkId);
                        }
                        break;
                    case TOTAL_SPEND:
                        if (NumberUtil.isGreater(lastPrice, BigDecimal.ZERO)) {
                            finishTaskSub(activityTask, collectionId, userId, lastCount, lastPrice, taskType, linkType, linkId);
                        }
                        break;
                }
            }
        } catch (Exception e) {
            log.error("活动任务完成异常", e);
        } finally {
            lock.unlock();
        }
    }

    public void finishTaskSub(ActivityTask activityTask, Long collectionId, Long userId, int count, BigDecimal price, ActivityTaskTypeEnum taskType, LinkTypeEnums linkType, Long linkId) {


        DateTime now = DateTime.now();

        int lastCount = 0;
        BigDecimal lastPrice = BigDecimal.ZERO;
        if (!activityTask.getRepeatFinish() && activityTaskPlanDao.getFinishCount(activityTask.getId(), userId) > 0) {
            return;
        }
        if (activityTask.getType() == ActivityTaskTypeEnum.BUY_APPOINT && collectionId.longValue() != activityTask.getBuyCollectionId().longValue()) {
            return;
        }

        if (activityTask.getParticipants() == ParticipantsEnum.APPOINT && userCollectionDao.getByUserIdAndCollectionIdCount(userId, activityTask.getPartakeCollectionId()) == 0) {
            return;
        }
        ActivityTaskPlan goingTaskPlan = activityTaskPlanDao.getGoingTask(activityTask.getId(), userId);
        if (ObjectUtil.isNull(goingTaskPlan)) {
            goingTaskPlan = new ActivityTaskPlan();
            goingTaskPlan.setUserId(userId);
            goingTaskPlan.setTaskName(activityTask.getTaskName());
            goingTaskPlan.setActivityTaskId(activityTask.getId());
            goingTaskPlan.setTaskType(activityTask.getType());
            goingTaskPlan.setTaskCurrentPlan(BigDecimal.ZERO);
            switch (taskType) {
                case BUY_APPOINT:
                    goingTaskPlan.setTaskFinishCondition(BigDecimal.valueOf(activityTask.getBuyCount()));
                    break;
                case TOTAL_BUY:
                    goingTaskPlan.setTaskFinishCondition(BigDecimal.valueOf(activityTask.getBuyCount()));
                    break;
                case TOTAL_SPEND:
                    goingTaskPlan.setTaskFinishCondition(activityTask.getSpendPrice());
                    break;
            }
            goingTaskPlan.setTaskDate(now);
            if (activityTask.getTaskPlan().intValue() == 0) {
                goingTaskPlan.setTaskResetTime(now.offsetNew(DateField.YEAR, 100));
            } else {
                goingTaskPlan.setTaskResetTime(DateUtil.beginOfWeek(now.offsetNew(DateField.DAY_OF_MONTH, 7)));
            }
            goingTaskPlan.setStatus(false);
            goingTaskPlan.insert();
        }
        ActivityTaskPlanRecord activityTaskPlanRecord = new ActivityTaskPlanRecord();
        activityTaskPlanRecord.setActivityTaskId(activityTask.getId());
        activityTaskPlanRecord.setActivityTaskPlanId(goingTaskPlan.getId());
        activityTaskPlanRecord.setUserId(userId);
        activityTaskPlanRecord.setPlanTime(now);
        activityTaskPlanRecord.setLinkType(linkType);
        activityTaskPlanRecord.setLinkId(linkId);
        activityTaskPlanRecord.insert();
        switch (taskType) {

            case BUY_APPOINT:
                if (NumberUtil.isGreater(goingTaskPlan.getTaskCurrentPlan().add(BigDecimal.valueOf(count)), goingTaskPlan.getTaskFinishCondition())) {
                    lastCount = goingTaskPlan.getTaskCurrentPlan().add(BigDecimal.valueOf(count)).subtract(goingTaskPlan.getTaskFinishCondition()).intValue();
                }
                goingTaskPlan.setTaskCurrentPlan(NumberUtil.min(goingTaskPlan.getTaskCurrentPlan().add(BigDecimal.valueOf(count)), goingTaskPlan.getTaskFinishCondition()));
                break;
            case TOTAL_BUY:
                if (NumberUtil.isGreater(goingTaskPlan.getTaskCurrentPlan().add(BigDecimal.valueOf(count)), goingTaskPlan.getTaskFinishCondition())) {
                    lastCount = goingTaskPlan.getTaskCurrentPlan().add(BigDecimal.valueOf(count)).subtract(goingTaskPlan.getTaskFinishCondition()).intValue();
                }
                goingTaskPlan.setTaskCurrentPlan(NumberUtil.min(goingTaskPlan.getTaskCurrentPlan().add(BigDecimal.valueOf(count)), goingTaskPlan.getTaskFinishCondition()));
                break;
            case TOTAL_SPEND:
                if (NumberUtil.isGreater(goingTaskPlan.getTaskCurrentPlan().add(price), goingTaskPlan.getTaskFinishCondition())) {
                    lastPrice = goingTaskPlan.getTaskCurrentPlan().add(price).subtract(goingTaskPlan.getTaskFinishCondition());
                }
                goingTaskPlan.setTaskCurrentPlan(NumberUtil.min(goingTaskPlan.getTaskCurrentPlan().add(price), goingTaskPlan.getTaskFinishCondition()));
                break;
        }
        if (NumberUtil.isGreaterOrEqual(goingTaskPlan.getTaskCurrentPlan(), goingTaskPlan.getTaskFinishCondition())) {
            goingTaskPlan.setStatus(true);
            goingTaskPlan.setTaskFinishTime(now);
        }
        goingTaskPlan.updateById();

        //发放奖励
        if (goingTaskPlan.getStatus()) {
            sendPrize(activityTask, goingTaskPlan);
        }

        switch (taskType) {
            case BUY_APPOINT:
                if (lastCount > 0) {
                    finishTaskSub(activityTask,collectionId, userId, lastCount, lastPrice, taskType, linkType, linkId);
                }
                break;
            case TOTAL_BUY:
                if (lastCount > 0) {
                    finishTaskSub(activityTask,collectionId, userId, lastCount, lastPrice, taskType, linkType, linkId);
                }
                break;
            case TOTAL_SPEND:
                if (NumberUtil.isGreater(lastPrice, BigDecimal.ZERO)) {
                    finishTaskSub(activityTask,collectionId, userId, lastCount, lastPrice, taskType, linkType, linkId);
                }
                break;
        }


    }

    private void sendPrize(ActivityTask activityTask, ActivityTaskPlan taskPlan) {
        List<ActivityTaskPrize> prizes = activityTaskPrizeDao.getByTaskId(taskPlan.getActivityTaskId());
        for (ActivityTaskPrize prize : prizes) {
            ActivityTaskUserPrize userPrize = new ActivityTaskUserPrize();
            userPrize.setActivityTaskId(activityTask.getId());
            userPrize.setActivityPlanId(taskPlan.getId());
            userPrize.setUserId(taskPlan.getUserId());
            userPrize.setPrizeCount(prize.getPrize());
            userPrize.setPrizeName(prize.getEquityName());
            userPrize.insert();
            switch (prize.getType()) {
                case INTEGRAL:
                    userAssetsService.addIntegralByUserId(BigDecimal.valueOf(prize.getPrize()), taskPlan.getUserId(), CoinCategoryEnum.INTEGRAL, AssetChangeLogEnum.ACTIVITY_TASK, taskPlan.getId(), activityTask.getTaskName());
                    break;
                case PROPS:
                    EquityProps equityProps = equityPropsDao.getById(prize.getEquityId());
                    //todo 任务权益道具获取类型修复!!!
                    userEquityPropsService.addEquityPropsByUser(equityProps, taskPlan.getUserId(), UserEquityPropsGetTypeEnum.ACTIVITY_TASK, prize.getPrize(), activityTask.getTaskName(), taskPlan.getId(),BigDecimal.ZERO,UserEquityPropsGetTypeEnum.ACTIVITY_TASK,taskPlan.getId());
                    break;
            }
        }
    }

    private void insertTaskPrize(List<ActivityTaskPrizeDTO> taskPrizeDTOS, ActivityTask task) {
        for (ActivityTaskPrizeDTO taskPrizeDTO : taskPrizeDTOS) {

            switch (taskPrizeDTO.getType()) {
                case INTEGRAL:
                    ActivityTaskPrize taskPrize = new ActivityTaskPrize();
                    taskPrize.setEquityName(taskPrizeDTO.getType().getDescription());
                    taskPrize.setType(taskPrizeDTO.getType());
                    taskPrize.setTaskId(task.getId());
                    taskPrize.setPrize(taskPrizeDTO.getPrize());

                    taskPrize.insert();
                    break;
                case PROPS:
                    EquityProps equityProps = equityPropsDao.getById(taskPrizeDTO.getPropsId());
                    if (ObjectUtil.isNull(equityProps)) {
                        throw new GlobalRunTimeException("奖品不存在");
                    }
                    taskPrize = new ActivityTaskPrize();
                    taskPrize.setType(taskPrizeDTO.getType());
                    Assert.isTrue(equityProps.getStatus(), () -> new GlobalRunTimeException("奖品已下架"));
                    taskPrize.setTaskId(task.getId());
                    taskPrize.setEquityName(equityProps.getFirstTitle());
                    taskPrize.setEquityId(equityProps.getId());
                    taskPrize.setPrize(taskPrizeDTO.getPrize());
                    taskPrize.insert();
                    break;
                default:
                    throw new GlobalRunTimeException("奖品类型不正确");
            }
        }
    }


}
